<template>
  <el-input v-model="searchText" placeholder="请输入个人id或昵称" style="width: 200px; margin-right: 10px" />
  <el-button @click="handleClick" type="primary">
    <el-icon style="vertical-align: middle">
      <Search />
    </el-icon>
    <span style="vertical-align: middle"> Search </span>
  </el-button>

  <el-table :data="tableData" style="width: 100%">
    <el-table-column fixed prop="userId" label="用户ID" width="200" />
    <el-table-column prop="name" label="用户昵称" width="200" />
    <el-table-column prop="phone" label="电话" width="200" />
    <el-table-column prop="birthdate" label="出生年月" width="200" />
    <el-table-column prop="degree" label="学历" width="200" />
    <el-table-column fixed="right" label="Operations" min-width="200">
      <template #default="scope">
        <el-button @click="detail(scope.row.userId)" link type="primary" size="small">
          详细信息
        </el-button>
        <el-button @click="open(scope.row.userId)" link type="danger" size="small">
          升级
        </el-button>

      </template>
    </el-table-column>
  </el-table>


  <el-dialog
      v-model="dialogVisible"
      title="简历详情"
      width="50%"
      custom-class="custom-resume-dialog"
  >
    <template #header>
      <div class="dialog-header">
        <div class="header-info">
          <span>{{ currentResume.name }}，{{ currentResume.birthdate }}</span>
          <el-tag size="medium" type="success">{{ currentResume.degree }}</el-tag>
        </div>
      </div>
    </template>

    <div class="dialog-body">
      <div class="info-row">
        <div class="info-col">
          <i class="el-icon-school"></i>
          <div class="label-bold-blue">学校：</div>
          {{ currentResume.school }}
        </div>
        <div class="info-col">
          <i class="el-icon-coin"></i>
          <div class="label-bold-blue">专业：</div>
          {{ currentResume.specialized }}
        </div>
      </div>

      <div class="info-row">
        <div class="info-col">
          <i class="el-icon-phone"></i>

          <div class="label-bold-blue">求职意向：</div>
          {{ currentResume.intention }}
        </div>
        <div class="info-col">
          <i class="el-icon-office-building"></i>
          <div class="label-bold-blue">工作经验：</div>
          {{ currentResume.experiment }}
        </div>
      </div>

      <hr>
      <div class="info-row full-width"> <!-- 技能独占一行 -->
        <i class="el-icon-skill"></i>
        <div class="label-bold-blue">专业技能：</div>
        <br>
        {{ currentResume.skills }}
      </div>
      <hr>
      <div class="info-row full-width"> <!-- 项目经验独占一行 -->
        <i class="el-icon-document"></i>
        <div class="label-bold-blue">项目经历：</div>
        <br>
        <ul>
          <li v-for="(project, index) in currentResume.projects.split('\n')" :key="index">{{ project }}</li>
        </ul>
      </div>


      <div class="info-row">
        <div class="info-col full-width"> <!-- 修改为独占一行 -->
          <i class="el-icon-suitcase"></i>
          <div class="label-bold-blue">联系电话：</div>
          {{ currentResume.phone }}
        </div>
      </div>

    </div>
  </el-dialog>

  <el-pagination
      v-model:current-page="pageNum"
      v-model:page-size="pageSize"
      :page-sizes="[10, 15, 20, 25, 30]"
      layout="jumper, total, sizes, prev, pager, next"
      background
      :total="total"
      @size-change="onSizeChange"
      @current-change="onCurrentChange"
      style="margin-top: 20px;

              justify-content: flex-end" />

</template>

<script setup>
import { Search } from '@element-plus/icons-vue'
import { ref } from 'vue'
import {getAllUsersService, updateUserService, getResumeByIdService} from "@/api/user.js";
import { ElMessage, ElMessageBox } from 'element-plus'

const dialogVisible = ref(false); // 控制对话框的显示隐藏
const currentResume = ref(null); // 存储当前选中的简历信息

const tableData = ref([{
  userId: '123456',
  name: '姓名',
  birthdate: '出生日期',
  degree: '学历',
  phone: '联系电话',
}])

const pageNum = ref(1)
const total = ref(20)
const pageSize = ref(15)
const searchText = ref('')

const searchForm = ref({
  userId: '',
  name: '',
  pageNum: pageNum.value,
  pageSize: pageSize.value
});

const detail = async (userId) => {
  let res = await getResumeByIdService(userId);
  currentResume.value = res.data
  dialogVisible.value = true;
}

async function handleClick() {
  //判断输入的是否为数字
  if(searchText.value !== ''){
    if(!isNaN(searchText.value)) {
      searchForm.value.userId = searchText.value
    }else {
      searchForm.value.name = searchText.value
    }
  }else {
    searchForm.value.userId = ''
    searchForm.value.name = ''
  }
  searchForm.value.pageNum = pageNum.value
  searchForm.value.pageSize = pageSize.value
  console.log(searchForm.value)
  let result = await getAllUsersService(searchForm.value);
  tableData.value = result.data.item;
  total.value = result.data.total
}

//首页搜索
async function GET_ALL_USERS() {
  searchForm.value.pageNum = pageNum.value
  searchForm.value.pageSize = pageSize.value
  let result = await getAllUsersService(searchForm.value);
  tableData.value = result.data.item;
  total.value = result.data.total
}

GET_ALL_USERS()

const onSizeChange = (size) => {
  pageSize.value = size
  GET_ALL_USERS()
}

const onCurrentChange = (num) => {
  pageNum.value = num;
  GET_ALL_USERS()
}

const open = async (userId) => {
  ElMessageBox.confirm(
      '您确定要升级该用户嘛?',
      'Warning',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }
  )
      .then(() => {
        console.log('userId', userId)
        updateUserService(userId)
        ElMessage({
          type: 'success',
          message: '升级成功',
        })
      })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: '取消升级',
        })
      })
      .finally(() => {
        GET_ALL_USERS()
      })
}
</script>

<style scoped>

</style>